package com.tusuo.stds.ui.similarity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;

import com.hannesdorfmann.mosby.mvp.MvpBasePresenter;
import com.tusuo.stds.Constants;
import com.tusuo.stds.MyApp;
import com.tusuo.stds.R;
import com.tusuo.stds.di.DaggerScope;
import com.tusuo.stds.di.Injector;
import com.tusuo.stds.rest.ApiHelper;
import com.tusuo.stds.rest.ApiResult;
import com.tusuo.stds.rest.SimilarityDetail;
import com.tusuo.stds.rest.SimilarityInfo;
import com.tusuo.stds.util.DESedeUtil;
import com.tusuo.stds.util.Utils;

import javax.inject.Inject;

import autodagger.AutoComponent;
import autodagger.AutoInjector;
import rx.Subscriber;

/**
 * Created by developer on 17-6-16.
 */
@AutoComponent(
        dependencies = MyApp.class
)
@AutoInjector
@DaggerScope(SimilarityTempPresenter.class)
public class SimilarityTempPresenter extends MvpBasePresenter<SimilarityTempView>{

    @Inject
    ApiHelper mApiHelper;
    @Inject
    SharedPreferences mSharedPref;
    private GetSimilarityDetailSubscriber mGetSimilarityDetailSubscriber;
    private Activity mActivity;
    private boolean mFinalQuestion;
    private SubmitSimilarityAnswerSubscriber mSubmitSimilarityAnswerSubscriber;

    private Subscriber<SimilarityInfo> mGetSimilarityInfoSubscriber = null;
    private String mErrMsgServer;

    SimilarityTempPresenter(Activity activity) {
        mActivity = activity;
        Injector.initSimilarityTempPresenterComponent().inject(this);
        mErrMsgServer = mActivity.getResources().getString(R.string.err_msg_server);
    }

    @Override
    public void attachView(SimilarityTempView view) {
        super.attachView(view);
        if (getView() != null) {
            getView().initFirst();
            if (mSharedPref == null) {
                mSharedPref = PreferenceManager.getDefaultSharedPreferences(mActivity);
            }
            getView().setIsTodayFinished(mSharedPref.getLong(Constants.KEY_SIMILARITY_QUESTION_DATE,0));
            getView().setSecretKey(DESedeUtil.getSecretKey(mSharedPref.getString(Constants.KEY_COOKIE, "")));

            mGetSimilarityDetailSubscriber = new GetSimilarityDetailSubscriber();
            mApiHelper.getSimilarityDetail(mGetSimilarityDetailSubscriber);

            mGetSimilarityInfoSubscriber = new GetSimilarityInfoSubscriber();
            mApiHelper.getSimilarityInfo(mGetSimilarityInfoSubscriber);
        }
    }

    public void submitAnswer(String answerContent,int questionId,long elapseSec,boolean isFinal) {
        mFinalQuestion = isFinal;
        mSubmitSimilarityAnswerSubscriber = new SubmitSimilarityAnswerSubscriber(answerContent, questionId, elapseSec, isFinal);
        mApiHelper.submitSimilarityAnswer(mSubmitSimilarityAnswerSubscriber,
                questionId,
                elapseSec,
                answerContent);
    }

    private class GetSimilarityDetailSubscriber extends Subscriber<SimilarityDetail> {
        @Override
        public void onCompleted() {}

        @Override
        public void onError(Throwable e) {
            Utils.UI.showToastShort(mErrMsgServer);
        }

        @Override
        public void onNext(final SimilarityDetail similarityDetail) {
            if (getView() != null) {
                getView().setSimilarity(similarityDetail);
            }
        }
    }

    private class SubmitSimilarityAnswerSubscriber extends Subscriber<ApiResult> {
        private int questionId;
        private long elapseSec;
        private boolean isFinal;
        private String answerContent;

        SubmitSimilarityAnswerSubscriber(String answerContent, int questionId, long elapseSec, boolean isFinal) {
            this.answerContent = answerContent;
            this.questionId = questionId;
            this.elapseSec = elapseSec;
            this.isFinal = isFinal;
        }

        @Override
        public void onCompleted() {
            if (mFinalQuestion) {
                Intent intent = new Intent(mActivity,SimilarityResultActivity_.class);
                mActivity.startActivity(intent);
                mActivity.finish();
            }
        }

        @Override
        public void onError(Throwable e) {
            Utils.UI.showToastShort(mErrMsgServer);
            if (getView() != null) {
                getView().requestSubmitQuestion(questionId,elapseSec,isFinal,answerContent);
            }
        }

        @Override
        public void onNext(final ApiResult result) {

            if (getView() != null) {
                getView().toNextQuestion();

            }
        }
    }

    private class GetSimilarityInfoSubscriber extends Subscriber<SimilarityInfo> {
        @Override
        public void onCompleted() {}

        @Override
        public void onError(Throwable e) {
            Utils.UI.showToastShort(mErrMsgServer);
        }

        @Override
        public void onNext(final SimilarityInfo similarityInfo) {
            if (similarityInfo.checkState(mActivity) && getView() != null) {
                getView().setSimilarityResult(similarityInfo);
            }
        }
    }
}
